<?php
namespace Apipad\Controller;
use Think\Controller;

class UserTuanDetailController extends ComController{



    //批量添加信息
    function batchAdd(){
        if(!I('did')){
            $this->ajaxErrReturn('部门信息id必填');
        }
        $d_model = M('user_tuan_dept');
        $d_wdata['id'] = I('did');
        $d_wdata['uid'] = UID;
        $d_vo = $d_model->where($d_wdata)->find();
        if(!$d_vo){
            $this->ajaxErrReturn('部门信息查询失败');
        }
        $model = M('UserTuanDetail');
        $model->startTrans();
        $post = I('post.');
        foreach($post['names'] as $key=>$name){
            if(!$name && $post['number'][$key]<1){
                continue;
            }
            $data['uid'] = UID;
            $data['tid'] = $d_vo['tid'];
            $data['did'] = $d_vo['id'];
            $data['age'] = $post['age'][$key] ? : '';
            $data['names'] = $post['names'][$key] ? : '';
            $data['sex'] = $post['sex'][$key] ? : 0;
            $data['height'] = $post['height'][$key] ? : '';
            $data['weight'] = $post['weight'][$key] ? : '';
            $data['xmcm'] = $post['xmcm'][$key] ? : '';
            $data['xfcm_qcy'] = $post['xfcm_qcy'][$key] ? : '';
            $data['xfcm_hzy'] = $post['xfcm_hzy'][$key] ? : '';
            //$data['xfcm_jxw'] = $post['xfcm_jxw'][$key] ? : '';
            $data['xfcm_cyxw'] = $post['xfcm_cyxw'][$key] ? : '';
            $data['xfcm_yw'] = $post['xfcm_yw'][$key] ? : '';
            $data['xfcm_zy'] = $post['xfcm_zy'][$key] ? : '';
            $data['xfcm_jk'] = $post['xfcm_jk'][$key] ? : '';
            $data['xfcm_xc'] = $post['xfcm_xc'][$key] ? : '';
            $data['xkcm'] = $post['xkcm'][$key] ? : '';
           // $data['xkcm_jyw'] = $post['xkcm_jyw'][$key] ? : '';
            $data['xkcm_cyyw'] = $post['xkcm_cyyw'][$key] ? : '';
            //$data['xkcm_jtw'] = $post['xkcm_jtw'][$key] ? : '';
            $data['xkcm_cytw'] = $post['xkcm_cytw'][$key] ? : '';
            $data['xkcm_hd'] = $post['xkcm_hd'][$key] ? : '';
            $data['xkcm_qkc'] = $post['xkcm_qkc'][$key] ? : '';
            $data['number'] = $post['number'][$key] ? : '';
            $data['remark'] = $post['remark'][$key] ? : '';
             //新增
            $data['unit'] = $post['unit'][$key]?:"斤";
            $data['xiuwei'] = $post['xiuwei'][$key]? : ''; 
            $data['xiabai'] = $post['xiabai'][$key]? : ''; 
            $data['linwei'] = $post['linwei'][$key]? : ''; 
            $data['tuiwei'] = $post['tuiwei'][$key]? : ''; 
            $data['jiaokou'] = $post['jiaokou'][$key]? : ''; 
            if(!$post['id'][$key]){
                $data['addtime'] = date('Y-m-d H:i:s');
                $data['add_time'] = NOW_TIME;
                $id = $model->add($data);
                if(!$id){
                    $this->ajaxErrReturn('添加数据失败');
                }
            }else{
                $wdata['id'] = $post['id'][$key];
                $wdata['uid'] = UID;
                $res = $model->where($wdata)->save($data);
                if($res===false){
                    $this->ajaxErrReturn('添加数据失败');
                }
            }
        }
        $model->commit();
        $this->ajaxSucReturn('数据提交成功');
    }

    //根据性别 身高 体重  品类查询信息
    public function get_mysqlcc(){
        $detail = I('post.');
        $user_mysql_list=M("user_mysql_list"); 
        $user_mysql_listc=M("user_mysql_listc"); 
        $sex=$detail['sex']?:1;
        $high=$detail['high']?:'';
        $cate_id=1;//固定西服大类
        $weight = $detail['weight']?:'';
        if(empty($high)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入身高";
            exit(json_encode($data_cc));
        }
        if(empty($cate_id)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "cate_id参数错误";
            exit(json_encode($data_cc));
        }
        //根据大类ID 查询上衣或者裤子
        $user_mysql_cate=M("user_mysql_default_cate"); 
        $mysql_child_count = $user_mysql_cate->where(['pid'=>$cate_id,'isdel'=>0])->count();
        $uid=UID;
        if($mysql_child_count>0){

            $jo_res = $weight % 2;
            if($jo_res != 0){
                $weight = bcadd($weight,1);
            }

            $cate_id2=$cate_id1=0;
            if($cate_id){
                $cate_id1=$user_mysql_cate->where(['pid'=>$cate_id,'stype'=>1,'sht_c'=>0,'isdel'=>0])->getField('id');
                $cate_id2=$user_mysql_cate->where(['pid'=>$cate_id,'stype'=>1,'sht_c'=>1,'isdel'=>0])->getField('id');
            }
            //衣服
            $where['isdel']=0;
            $where['uid']=$uid;
            $where['sex']=$sex;
            $where['high']=$high;
            $where['cat_id']=$cate_id1;
            $where['weight'] = $weight;
            //裤子 
            $fyst['isdel']=0;
            $fyst['uid']=$uid;
            $fyst['sex']=$sex;
            $fyst['high']=$high;
            $fyst['cat_id']=$cate_id2;
            $fyst['weight'] = $weight;
            $result['shagnyi_info']=$user_mysql_list->where($where)->find();
            $result['kuzi_info']=$user_mysql_listc->where($fyst)->find();
        }else{
            $cate_info = $user_mysql_cate->where(['id'=>$cate_id,'stype'=>1,'isdel'=>0])->find();
            if($cate_info['sht_c'] == 1){
                //下装
                $fyst['isdel']=0;
                $fyst['uid']=$uid;
                $fyst['sex']=$sex;
                $fyst['high']=$high;
                $fyst['cat_id']=$cate_id;
                $fyst['weight'] = $weight;
                $result['kuzi_info']=$user_mysql_listc->where($fyst)->find();
                $result['shagnyi_info'] = [];
            }else{
                //上装
                $where['isdel']=0;
                $where['uid']=$uid;
                $where['sex']=$sex;
                $where['high']=$high;
                $where['cat_id']=$cate_id;
                $where['weight'] = $weight;
                $result['kuzi_info']=[];
                $result['shagnyi_info'] = $user_mysql_list->where($where)->find();
            }
        }
        $data_cc['code']=200;
        $data_cc['msg']='查询成功';
        $data_cc['info']=$result;
        exit(json_encode($data_cc));
    }
}